Symptom Comparisons

Row

One Test

Row

Two Tests - Test 1

Two Tests - Test 2

Row

Three Tests - Test 1

Three Tests - Test 2

Three Tests - Test 3

Row

Four Tests - Test 1

Four Tests - Test 2

Four Tests - Test 3

Four Tests - Test 4

Cluster Comparisons

Row

1 Test Cluster Summary

1 Test Cluster Boxplot

1 Test Cluster ANOVA

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   56.5  11.296     529 <2e-16 ***
Residuals       59940 1279.9   0.021                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

1 Test Cluster PWC

Row

2 Tests Cluster Summary - Test 1

2 Tests Cluster Boxplot - Test 1

2 Tests Cluster ANOVA - Test 1

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   42.6   8.516   330.4 <2e-16 ***
Residuals       31764  818.5   0.026                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2 Tests Cluster PWC - Test 1

2 Tests Cluster Summary - Test 2

2 Tests Cluster Boxplot - Test 2

2 Tests Cluster ANOVA - Test 2

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   7.22  1.4450   173.7 <2e-16 ***
Residuals       31764 264.22  0.0083                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2 Tests Cluster PWC - Test 2

Row

3 Tests Cluster Summary - Test 1

3 Tests Cluster Boxplot - Test 1

3 Tests Cluster ANOVA - Test 1

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   23.7   4.747   151.2 <2e-16 ***
Residuals       13362  419.4   0.031                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3 Tests Cluster PWC - Test 1

3 Tests Cluster Summary - Test 2

3 Tests Cluster Boxplot - Test 2

3 Tests Cluster ANOVA - Test 2

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   6.96  1.3910   110.3 <2e-16 ***
Residuals       13362 168.51  0.0126                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3 Tests Cluster PWC - Test 2

3 Tests Cluster Summary - Test 3

3 Tests Cluster Boxplot - Test 3

3 Tests Cluster ANOVA - Test 3

                   Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster     5   0.83 0.16671   33.59 <2e-16 ***
Residuals       13362  66.32 0.00496                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3 Tests Cluster PWC - Test 3

Row

4 Tests Cluster Summary - Test 1

4 Tests Cluster Boxplot - Test 1

4 Tests Cluster ANOVA - Test 1

                  Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster    5  11.15  2.2301    54.9 <2e-16 ***
Residuals       4674 189.87  0.0406                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 1

4 Tests Cluster Summary - Test 2

4 Tests Cluster Boxplot - Test 2

4 Tests Cluster ANOVA - Test 2

                  Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster    5   5.62  1.1240   57.88 <2e-16 ***
Residuals       4674  90.76  0.0194                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 2

4 Tests Cluster Summary - Test 3

4 Tests Cluster Boxplot - Test 3

4 Tests Cluster ANOVA - Test 3

                  Df Sum Sq Mean Sq F value Pr(>F)    
symptom_cluster    5   0.91 0.18163   19.14 <2e-16 ***
Residuals       4674  44.34 0.00949                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 3

4 Tests Cluster Summary - Test 4

4 Tests Cluster Boxplot - Test 4

4 Tests Cluster ANOVA - Test 4

                  Df Sum Sq Mean Sq F value   Pr(>F)    
symptom_cluster    5   0.27 0.05395   7.113 1.23e-06 ***
Residuals       4674  35.45 0.00758                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Tests Cluster PWC - Test 4

Gender-Cluster Interaction

Row

1 Test Cluster Summary

1 Test Cluster Boxplot

1 Test Cluster Two-Way ANOVA

1 Test Cluster PWC

Row

2 Tests Cluster Summary - Test 1

2 Tests Cluster Boxplot - Test 1

2 Tests Cluster Two-Way ANOVA - Test 1

2 Tests Cluster PWC - Test 1

2 Tests Cluster Summary - Test 2

2 Tests Cluster Boxplot - Test 2

2 Tests Cluster Two-Way ANOVA - Test 2

2 Tests Cluster PWC - Test 1

Row

3 Tests Cluster Summary - Test 1

3 Tests Cluster Boxplot - Test 1

3 Tests Cluster Two-Way ANOVA - Test 1

3 Tests Cluster PWC - Test 1

3 Tests Cluster Summary - Test 2

3 Tests Cluster Boxplot - Test 2

3 Tests Cluster Two-Way ANOVA - Test 2

3 Tests Cluster PWC - Test 2

3 Tests Cluster Summary - Test 3

3 Tests Cluster Boxplot - Test 3

3 Tests Cluster Two-Way ANOVA - Test 3

3 Tests Cluster PWC - Test 3

Row

4 Tests Cluster Summary - Test 1

4 Tests Cluster Boxplot - Test 1

4 Tests Cluster Two-Way ANOVA - Test 1

4 Tests Cluster PWC - Test 1

4 Tests Cluster Summary - Test 2

4 Tests Cluster Boxplot - Test 2

4 Tests Cluster Two-Way ANOVA - Test 2

4 Tests Cluster PWC - Test 2

4 Tests Cluster Summary - Test 3

4 Tests Cluster Boxplot - Test 3

4 Tests Cluster Two-Way ANOVA - Test 3

4 Tests Cluster PWC - Test 3

4 Tests Cluster Summary - Test 4

4 Tests Cluster Boxplot - Test 4

4 Tests Cluster Two-Way ANOVA - Test 4

4 Tests Cluster PWC - Test 4

---
title: "HCAMP Cluster Comparison"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
    vertical_layout: scroll
    theme: united
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(here)
library(janitor)
library(rio)
library(colorblindr)
library(gghighlight)
library(forcats)
library(ggrepel)
library(knitr)
library(kableExtra)
library(reactable)
library(plotly)
library(glue)
library(fs)
library(rstatix)
library(ggpubr)
library(writexl) 
library(remotes)
library(profvis) 


theme_set(theme_minimal(15) +
            theme(legend.position = "bottom",
                  panel.grid.major.x = element_line(color = "gray60"),
                  panel.grid.minor.x = element_blank(),
                  panel.grid.major.y = element_blank())
          )

```

```{r global, include=FALSE}
#all clean sims data
sims_concussion_data <- read_csv(here("data", "sims_concussion_data.csv"))

sims_concussion_data <- sims_concussion_data %>% 
  mutate(age = as.factor(age))

#impact data only

one_impact_test <- read_csv(here("data", "one_post_injury_impact_test.csv"))

two_impact_test <- read_csv(here("data", "two_post_injury_impact_test.csv"))

three_impact_test <- read_csv(here("data", "three_post_injury_impact_test.csv"))

four_impact_test <- read_csv(here("data", "four_post_injury_impact_test.csv"))

# impact sims merge data

one_impact_sims <- read_csv(here("data", "one_post_injury_test_impact_sims_merge.csv"))

two_impact_sims <- read_csv(here("data", "two_post_injury_test_impact_sims_merge.csv"))

three_impact_sims <- read_csv(here("data", "three_post_injury_test_impact_sims_merge.csv"))

four_impact_sims <- read_csv(here("data", "four_post_injury_test_impact_sims_merge.csv"))

# pcss items 

pcss_items <- import(here("data", "pcss_items.xlsx"),
               setclass = "tbl_df") 

pcss_items_data <- import(here("data", "pcss_items_data.xlsx"),
               setclass = "tbl_df") 

pcss_cluster <- import(here("data", "cluster_scores.xlsx"),
               setclass = "tbl_df") 
```


# Symptom Comparisons

Sidebar {.sidebar}
------------

Symptoms that appear to be rated with higher severity more consistently: 

  1. Headache 
  2. Difficulty concentrating 
  3. Difficulty remembering 
  4. Irritability 
  5. Light sensitivity 
  
Symptoms from the headache-migraine, cognitive and anxiety-mood clusters appear to remain elevated throughout recovery. 

The vestibular cluster symptom of dizziness appears to be rated highest at the time of first testing and lessen in severity across the recovery time. 

```{r, include=FALSE}
one_impact_test_cluster <- one_impact_test %>% 
  pivot_longer(
    cols = c(32:37),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) %>% 
  select(c(1:8, 37, 38))

one_impact_test_symptoms <- one_impact_test %>% 
  pivot_longer(
    cols = c(9:30),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(c(1:15, 21, 22))

tst_aov <- aov(total_symptom_score_post_injury_1 ~ post_injury_1_rating,
               data = one_impact_test_symptoms)

summary(tst_aov)

one_impact_test_symptoms <- left_join(one_impact_test_symptoms, pcss_items_data)

```

```{r, include=FALSE}

two_impact_test_1 <- two_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_1"))

two_impact_test_2 <- two_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_2"))

two_impact_test_1_symptoms <- two_impact_test_1 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(-c(14:18))

two_impact_test_1_symptoms <- left_join(two_impact_test_1_symptoms, pcss_items_data)

two_impact_test_2_symptoms <- two_impact_test_2 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_2_rating",
    names_pattern = "(.*)_post_injury_2"
  ) %>% 
  select(-c(14:18))

two_impact_test_2_symptoms <- left_join(two_impact_test_2_symptoms, pcss_items_data)

```

```{r, include=FALSE}
three_impact_test_1 <- three_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_1"))

three_impact_test_2 <- three_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_2"))

three_impact_test_3 <- three_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_3"))

three_impact_test_1_symptoms <- three_impact_test_1 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(-c(14:18))

three_impact_test_1_symptoms <- left_join(three_impact_test_1_symptoms, pcss_items_data)


three_impact_test_2_symptoms <- three_impact_test_2 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_2_rating",
    names_pattern = "(.*)_post_injury_2"
  ) %>% 
  select(-c(14:18))

three_impact_test_2_symptoms <- left_join(three_impact_test_2_symptoms, pcss_items_data)

three_impact_test_3_symptoms <- three_impact_test_3 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_3_rating",
    names_pattern = "(.*)_post_injury_3"
  ) %>% 
  select(-c(14:18))

three_impact_test_3_symptoms <- left_join(three_impact_test_3_symptoms, pcss_items_data)

```

```{r, include=FALSE}
four_impact_test_1 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_1"))

four_impact_test_2 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_2"))

four_impact_test_3 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_3"))

four_impact_test_4 <- four_impact_test %>% 
  select(passport_id, gender, contains("_post_injury_4"))

four_impact_test_1_symptoms <- four_impact_test_1 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_1_rating",
    names_pattern = "(.*)_post_injury_1"
  ) %>% 
  select(-c(14:18))

four_impact_test_1_symptoms <- left_join(four_impact_test_1_symptoms, pcss_items_data)


four_impact_test_2_symptoms <- four_impact_test_2 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_2_rating",
    names_pattern = "(.*)_post_injury_2"
  ) %>% 
  select(-c(14:18))

four_impact_test_2_symptoms <- left_join(four_impact_test_2_symptoms, pcss_items_data)

four_impact_test_3_symptoms <- four_impact_test_3 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_3_rating",
    names_pattern = "(.*)_post_injury_3"
  ) %>% 
  select(-c(14:18))

four_impact_test_3_symptoms <- left_join(four_impact_test_3_symptoms, pcss_items_data)


four_impact_test_4_symptoms <- four_impact_test_4 %>% 
  pivot_longer(
    cols = c(7:28),
    names_to = "symptom",
    names_prefix = "pcss_",
    values_to = "post_injury_4_rating",
    names_pattern = "(.*)_post_injury_4"
  ) %>% 
  select(-c(14:18))

four_impact_test_4_symptoms <- left_join(four_impact_test_4_symptoms, pcss_items_data)
```


Row {.tabset}
-----------------------------------------------------------------------

### One Test 

```{r, include=FALSE}
all_symptom_tbl <- one_impact_test_symptoms %>% 
    group_by(gender, cluster, symptom) %>% 
    summarize(Mean = mean(post_injury_1_rating),
              SD = sd(post_injury_1_rating),
              Min = min(post_injury_1_rating),
              Max = max(post_injury_1_rating),
              Total = length(post_injury_1_rating)) %>% 
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        cluster = colDef(name = "Symptom Cluster"),
        symptom = colDef(name = "Symptom"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE)),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )

symptom_tbl <- function(df, a, b, c, d) {
    df %>% 
    group_by({{a}}, {{b}}, {{c}}) %>% 
    summarize(Mean = mean({{d}}),
              SD = sd({{d}}),
              Min = min({{d}}),
              Max = max({{d}}),
              Total = length({{d}})) %>% 
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        cluster = colDef(name = "Symptom Cluster"),
        symptom = colDef(name = "Symptom"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE)),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )
}

symptom_tbl_over_three <- function(df, a, b, c, d) {
    df %>% 
    group_by({{a}}, {{b}}, {{c}}) %>% 
    filter({{d}} >=3) %>% 
    summarize(Mean = mean({{d}}),
              SD = sd({{d}}),
              Min = min({{d}}),
              Max = max({{d}}),
              Total = length({{d}})) %>% 
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        cluster = colDef(name = "Symptom Cluster"),
        symptom = colDef(name = "Symptom"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE)),
        Total = colDef(format = colFormat(separators = TRUE, suffix = " concussions"))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )
}

```


```{r, include=TRUE}
symptom_tbl(one_impact_test_symptoms, gender, cluster, symptom, post_injury_1_rating)
```


Row {.tabset}
-----------------------------------------------------------------------

### Two Tests - Test 1

```{r, include=TRUE}
symptom_tbl(two_impact_test_1_symptoms, gender, cluster, symptom, post_injury_1_rating)
```

### Two Tests - Test 2 

```{r, include=TRUE}
symptom_tbl(two_impact_test_2_symptoms, gender, cluster, symptom, post_injury_2_rating)
```


Row {.tabset}
-----------------------------------------------------------------------

### Three Tests - Test 1

```{r, include=TRUE}
symptom_tbl(three_impact_test_1_symptoms, gender, cluster, symptom, post_injury_1_rating)
```

### Three Tests - Test 2 

```{r, include=TRUE}
symptom_tbl(three_impact_test_2_symptoms, gender, cluster, symptom, post_injury_2_rating)
```

### Three Tests - Test 3 

```{r, include=TRUE}
symptom_tbl(three_impact_test_3_symptoms, gender, cluster, symptom, post_injury_3_rating)
```



Row {.tabset}
-----------------------------------------------------------------------

### Four Tests - Test 1

```{r, include=TRUE}
symptom_tbl(four_impact_test_1_symptoms, gender, cluster, symptom, post_injury_1_rating)
```

### Four Tests - Test 2 

```{r, include=TRUE}
symptom_tbl(four_impact_test_2_symptoms, gender, cluster, symptom, post_injury_2_rating)
```

### Four Tests - Test 3

```{r, include=TRUE}
symptom_tbl(four_impact_test_3_symptoms, gender, cluster, symptom, post_injury_3_rating)
```

### Four Tests - Test 4

```{r, include=TRUE}
symptom_tbl(four_impact_test_4_symptoms, gender, cluster, symptom, post_injury_4_rating)
```

# Cluster Comparisons 

Sidebar {.sidebar}
------------

Symptom Cluster Potential Symptom Severity Range:

  1. Headache-Migraine Cluster: 0 - 18 (3 symptoms)
  2. Cognitive Cluster: 0 - 24 (4 symptoms)
  3. Anxiety-Mood Cluster: 0 - 30 (5 symptoms)
  4. Ocular-Motor Cluster: 0 - 6 (1 symptom)
  5. Vestibular Cluster: 0 - 24 (4 symptoms)
  6. Sleep Cluster: 0 - 30 (5 symptoms)
  

To compare symptom cluster severity scores, each cluster was rescaled for the distribution of scores to fall between 0 to 1. Each cluster was rescaled individually and then grouped together into the same variable corresponding to post-injury test to compare with one-way ANOVAs. 

**One Test Summary**

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor and cognitive/sleep
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters
  * Cognitive and sleep clusters tied for second highest rated clusters
  * Anxiety-mood and ocular-motor rated the lowest 


**Two Tests Summary**

_Test One:_

  * Non-significant cluster comparisons: cognitive/sleep 
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 
  * Cognitive and sleep clusters tied for second highest rated clusters 
  * Anxiety-mood and ocular-motor rated the lowest 

_Test Two:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 

**Three Tests Summary**

_Test One:_

  * Non-significant cluster comparisons: anxiety-mood/vestibular 
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters

_Test Two:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular
  * Headache-migraine cluster significantly higher than all other clusters 
  
_Test Three:_

  * Non-significant cluster comparisons: cognitive/headache-migraine, anxiety-mood/ocular-motor, cognitive/sleep, headache-migraine/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular
  * Headache-migraine, cognitive, and sleep clusters significantly higher than other clusters 

**Four Tests Summary**

_Test One:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, cognitive/vestibular, and sleep/vestibular
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 
  * Cognitive, vestibular, and sleep clusters not significantly different than each other and represent second set of clusters behind headache-migraine 
  
_Test Two:_

  * Non-significant cluster comparisons: anxiety-mood/ocular-motor, cognitive/sleep, and ocular-motor/vestibular
  * All other PWC were significant 
  * Headache-migraine cluster significantly higher than all other clusters 

_Test Three:_ 

  * Non-significant cluster comparisons: cognitive/headache-migraine, anxiety-mood/ocular-motor, cognitive/sleep, anxiety-mood/vestibular, and ocular-motor/vestibular 
  * All other PWC were significant 
  * Headache-migraine cluster rated the highest but not significantly different than cognitive cluster 
  * Cognitive cluster rated second highest but not significantly different than sleep cluster 
_Test Four:_

  * Non-significant cluster comparisons: anxiety-mood/cognitive, cognitive/headache-migraine, anxiety-mood/ocular-motor, cognitive/ocular-motor, anxiety-mood/sleep, cognitive/sleep, headache-migraine/sleep, ocular-motor/sleep, anxiety-mood/vestibular, ocular-motor/vestibular, and sleep/vestibular 
  * Significant cluster comparisons: cognitive/vestibular, headache-migraine/vestibular, anxiety-mood/headache-migraine, ocular-motor/headache-migraine
  * Probably most important comparison from this test was the significant difference between anxiety-mood and headache-migraine severity ratings 
  
Row {.tabset}
-----------------------------------------------------------------------

### 1 Test Cluster Summary 

```{r, include=FALSE}
clusters_one_impact_test <- one_impact_test %>% 
  select(c(1:8, 32:37)) %>% 
  mutate(X1 = as.character(X1),
         row = as.character(row),
         age = as.character(age))

str(clusters_one_impact_test)

# normalize <- function(x) {
# return ((x - min(x)) / (max(x) - min(x)))
# }

rescale01b <- function(x) {
  if(is.numeric(x)) {
    z <- na.omit(x)
    min_z <- min(z)
    max_z <- max(z)
    (z - min_z) / (max_z - min_z)
  }
  else {
    return()
  }
}

rescale01b(c(0, 5, 10))

clusters_one_impact_test_scaled <- map_df(clusters_one_impact_test, rescale01b)

clusters_one_impact_test_scaled <- bind_cols(clusters_one_impact_test, 
                                             clusters_one_impact_test_scaled)

clusters_one_impact_test_scaled <- clusters_one_impact_test_scaled %>% 
  select(-c(9:14))


clusters_one_impact_test_scaled[sapply(clusters_one_impact_test_scaled, is.character)] <- lapply(clusters_one_impact_test_scaled[sapply(clusters_one_impact_test_scaled, is.character)], as.factor)

str(clusters_one_impact_test_scaled)

clusters_one_impact_test_scaled_long <- clusters_one_impact_test_scaled %>% 
    pivot_longer(
    cols = c(9:14),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 



str(clusters_one_impact_test_scaled_long)
```

```{r, include=FALSE}
#summary 
cluster_smry_tbl <- function(df, x) {
  df %>% 
  group_by(symptom_cluster) %>% 
  summarize(mean = mean({{x}}),
            sd = sd({{x}}),
            min = min({{x}}),
            max = max({{x}}),
            total = length({{x}})) %>%
  mutate(mean = as.numeric(mean),
         sd = as.numeric(sd),
         min = as.numeric(min),
         max = as.numeric(max),
         total = as.numeric(total)) %>% 
  reactable(
    columns = list(
      symptom_cluster = colDef(name = "Symptom Cluster",
                            align = "center"),
      mean = colDef(name = "Mean",
                    align = "center",
                    format = colFormat(digits = 2, separators = TRUE)),
      sd = colDef(name = "SD",
                  align = "center",
                  format = colFormat(digits = 2, separators = TRUE)),
      min = colDef(name = "Min",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      max = colDef(name = "Max",
                   align = "center",
                   format = colFormat(separators = TRUE)),
      total = colDef(name = "Total",
                     align = "center",
                   format = colFormat(separators = TRUE, suffix = " concussions"))),
    pagination = TRUE,
    striped = TRUE,
    outlined = TRUE,
    compact = TRUE,
    highlight = TRUE,
    bordered = TRUE
  )
}

cluster_smry_tbl(clusters_one_impact_test_scaled_long, post_injury_1_score)
```

```{r, include=TRUE}
cluster_smry_tbl(clusters_one_impact_test_scaled_long, post_injury_1_score)
```

### 1 Test Cluster Boxplot 

```{r, include=FALSE}
cluster_bxp_1 <- ggplot(clusters_one_impact_test_scaled_long,
                      aes(symptom_cluster, post_injury_1_score)) +
  geom_boxplot() + 
  labs(x = "Symptom Cluster",
       y = "Scaled Severity Score") +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 25))

cluster_bxp <- function(df, x, y) {
  ggplot(df, aes({{x}}, {{y}})) +
  geom_boxplot() + 
  labs(x = "Symptom Cluster",
       y = "Scaled Severity Score") +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 25))
}

cluster_bxp(clusters_one_impact_test_scaled_long, symptom_cluster, post_injury_1_score)

```

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_one_impact_test_scaled_long, symptom_cluster, post_injury_1_score))
```

### 1 Test Cluster ANOVA 

```{r, include=FALSE}
cluster_one_test_aov <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_one_impact_test_scaled_long)

summary(cluster_one_test_aov)

cluster_one_test_aov
```

```{r, include=TRUE}
summary(cluster_one_test_aov)
```

### 1 Test Cluster PWC

```{r, include=FALSE}
cluster_one_test_aov_pwc <- clusters_one_impact_test_scaled_long %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_one_test_aov_pwc <- cluster_one_test_aov_pwc %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_one_test_aov_pwc %>% 
  reactable()
```


Row {.tabset}
-----------------------------------------------------------------------

### 2 Tests Cluster Summary - Test 1

```{r, include=FALSE}

clusters_two_impact_test_1 <- two_impact_test %>% 
  select(c(1:12, contains("_post_injury_1"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2))

clusters_two_impact_test_1 <- clusters_two_impact_test_1 %>% 
  select(c(1:12, 36:41))

clusters_two_impact_test_1_scaled <- map_df(clusters_two_impact_test_1, rescale01b)

clusters_two_impact_test_1_scaled <- bind_cols(clusters_two_impact_test_1, 
                                             clusters_two_impact_test_1_scaled)

clusters_two_impact_test_1_scaled <- clusters_two_impact_test_1_scaled %>% 
  select(-c(13:18))


clusters_two_impact_test_1_scaled[sapply(clusters_two_impact_test_1_scaled, is.character)] <- lapply(clusters_two_impact_test_1_scaled[sapply(clusters_two_impact_test_1_scaled, is.character)], as.factor)

str(clusters_two_impact_test_1_scaled)

clusters_two_impact_test_1_long <- clusters_two_impact_test_1_scaled %>% 
    pivot_longer(
    cols = c(13:18),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 


clusters_two_impact_test_2 <- two_impact_test %>% 
  select(c(1:12, contains("_post_injury_2"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2))

clusters_two_impact_test_2 <- clusters_two_impact_test_2 %>% 
  select(c(1:12, 36:41))

clusters_two_impact_test_2_scaled <- map_df(clusters_two_impact_test_2, rescale01b)

clusters_two_impact_test_2_scaled <- bind_cols(clusters_two_impact_test_2, 
                                             clusters_two_impact_test_2_scaled)

clusters_two_impact_test_2_scaled <- clusters_two_impact_test_2_scaled %>% 
  select(-c(13:18))


clusters_two_impact_test_2_scaled[sapply(clusters_two_impact_test_2_scaled, is.character)] <- lapply(clusters_two_impact_test_2_scaled[sapply(clusters_two_impact_test_2_scaled, is.character)], as.factor)

str(clusters_two_impact_test_2_scaled)

clusters_two_impact_test_2_long <- clusters_two_impact_test_2_scaled %>% 
    pivot_longer(
    cols = c(13:18),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  ) 

clusters_two_impact_test_complete <- left_join(clusters_two_impact_test_1_long,
                                               clusters_two_impact_test_2_long)
```

```{r, include=TRUE}
cluster_smry_tbl(clusters_two_impact_test_complete, post_injury_1_score)
```

### 2 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_two_impact_test_complete, 
                     symptom_cluster, post_injury_1_score))
```

### 2 Tests Cluster ANOVA - Test 1

```{r, include=FALSE}
cluster_two_tests_aov_1 <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_two_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_two_tests_aov_1)
```

### 2 Tests Cluster PWC - Test 1

```{r, include=FALSE}
cluster_two_tests_aov_pwc_1 <- clusters_two_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_two_tests_aov_pwc_1 <- cluster_two_tests_aov_pwc_1 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_two_tests_aov_pwc_1 %>% 
  reactable()
```


### 2 Tests Cluster Summary - Test 2

```{r, include=TRUE}
cluster_smry_tbl(clusters_two_impact_test_complete, post_injury_2_score)
```

### 2 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_two_impact_test_complete, 
                     symptom_cluster, post_injury_2_score))
```

### 2 Tests Cluster ANOVA - Test 2

```{r, include=FALSE}
cluster_two_tests_aov_2 <- aov(post_injury_2_score ~ symptom_cluster, 
                            data = clusters_two_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_two_tests_aov_2)
```

### 2 Tests Cluster PWC - Test 2

```{r, include=FALSE}
cluster_two_tests_aov_pwc_2 <- clusters_two_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_2_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_two_tests_aov_pwc_2 <- cluster_two_tests_aov_pwc_2 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_two_tests_aov_pwc_2 %>% 
  reactable()
```



Row {.tabset}
-----------------------------------------------------------------------

### 3 Tests Cluster Summary - Test 1

```{r, include=FALSE}

clusters_three_impact_test_1 <- three_impact_test %>% 
  select(c(1:16, contains("_post_injury_1"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3))

clusters_three_impact_test_1 <- clusters_three_impact_test_1 %>% 
  select(c(1:16, 40:45))

clusters_three_impact_test_1_scaled <- map_df(clusters_three_impact_test_1, rescale01b)

clusters_three_impact_test_1_scaled <- bind_cols(clusters_three_impact_test_1, 
                                             clusters_three_impact_test_1_scaled)

clusters_three_impact_test_1_scaled <- clusters_three_impact_test_1_scaled %>% 
  select(-c(17:22))

clusters_three_impact_test_1_scaled[sapply(clusters_three_impact_test_1_scaled, is.character)] <- lapply(clusters_three_impact_test_1_scaled[sapply(clusters_three_impact_test_1_scaled, is.character)], as.factor)

str(clusters_three_impact_test_1_scaled)

clusters_three_impact_test_1_long <- clusters_three_impact_test_1_scaled %>% 
    pivot_longer(
    cols = c(17:22),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 


clusters_three_impact_test_2 <- three_impact_test %>% 
  select(c(1:16, contains("_post_injury_2"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3))

clusters_three_impact_test_2 <- clusters_three_impact_test_2 %>% 
  select(c(1:16, 40:45))

clusters_three_impact_test_2_scaled <- map_df(clusters_three_impact_test_2, rescale01b)

clusters_three_impact_test_2_scaled <- bind_cols(clusters_three_impact_test_2, 
                                             clusters_three_impact_test_2_scaled)

clusters_three_impact_test_2_scaled <- clusters_three_impact_test_2_scaled %>% 
  select(-c(17:22))

clusters_three_impact_test_2_scaled[sapply(clusters_three_impact_test_2_scaled, is.character)] <- lapply(clusters_three_impact_test_2_scaled[sapply(clusters_three_impact_test_2_scaled, is.character)], as.factor)

str(clusters_three_impact_test_2_scaled)

clusters_three_impact_test_2_long <- clusters_three_impact_test_2_scaled %>% 
    pivot_longer(
    cols = c(17:22),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  ) 


clusters_three_impact_test_3 <- three_impact_test %>% 
  select(c(1:16, contains("_post_injury_3"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3))

clusters_three_impact_test_3 <- clusters_three_impact_test_3 %>% 
  select(c(1:16, 40:45))

clusters_three_impact_test_3_scaled <- map_df(clusters_three_impact_test_3, rescale01b)

clusters_three_impact_test_3_scaled <- bind_cols(clusters_three_impact_test_3, 
                                             clusters_three_impact_test_3_scaled)

clusters_three_impact_test_3_scaled <- clusters_three_impact_test_3_scaled %>% 
  select(-c(17:22))

clusters_three_impact_test_3_scaled[sapply(clusters_three_impact_test_3_scaled, is.character)] <- lapply(clusters_three_impact_test_3_scaled[sapply(clusters_three_impact_test_3_scaled, is.character)], as.factor)

str(clusters_three_impact_test_3_scaled)

clusters_three_impact_test_3_long <- clusters_three_impact_test_3_scaled %>% 
    pivot_longer(
    cols = c(17:22),
    names_to = "symptom_cluster",
    values_to = "post_injury_3_score",
    names_pattern = "(.*)_score_post_injury_3"
  ) 

clusters_three_impact_test_join_1 <- left_join(clusters_three_impact_test_1_long,
                                               clusters_three_impact_test_2_long)

clusters_three_impact_test_complete <- left_join(clusters_three_impact_test_join_1, 
                                                 clusters_three_impact_test_3_long)
```


```{r, include=TRUE}
cluster_smry_tbl(clusters_three_impact_test_complete, post_injury_1_score)
```

### 3 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_three_impact_test_complete, 
                     symptom_cluster, post_injury_1_score))
```

### 3 Tests Cluster ANOVA - Test 1

```{r, include=FALSE}
cluster_three_tests_aov_1 <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_three_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_three_tests_aov_1)
```

### 3 Tests Cluster PWC - Test 1

```{r, include=FALSE}
cluster_three_tests_aov_pwc_1 <- clusters_three_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_three_tests_aov_pwc_1 <- cluster_three_tests_aov_pwc_1 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_three_tests_aov_pwc_1 %>% 
  reactable()
```


### 3 Tests Cluster Summary - Test 2

```{r, include=TRUE}
cluster_smry_tbl(clusters_three_impact_test_complete, post_injury_2_score)
```

### 3 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_three_impact_test_complete, 
                     symptom_cluster, post_injury_2_score))
```

### 3 Tests Cluster ANOVA - Test 2

```{r, include=FALSE}
cluster_three_tests_aov_2 <- aov(post_injury_2_score ~ symptom_cluster, 
                            data = clusters_three_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_three_tests_aov_2)
```

### 3 Tests Cluster PWC - Test 2

```{r, include=FALSE}
cluster_three_tests_aov_pwc_2 <- clusters_three_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_2_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_three_tests_aov_pwc_2 <- cluster_three_tests_aov_pwc_2 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_three_tests_aov_pwc_2 %>% 
  reactable()
```


### 3 Tests Cluster Summary - Test 3

```{r, include=TRUE}
cluster_smry_tbl(clusters_three_impact_test_complete, post_injury_3_score)
```

### 3 Tests Cluster Boxplot - Test 3

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_three_impact_test_complete, 
                     symptom_cluster, post_injury_3_score))
```

### 3 Tests Cluster ANOVA - Test 3

```{r, include=FALSE}
cluster_three_tests_aov_3 <- aov(post_injury_3_score ~ symptom_cluster, 
                            data = clusters_three_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_three_tests_aov_3)
```

### 3 Tests Cluster PWC - Test 3

```{r, include=FALSE}
cluster_three_tests_aov_pwc_3 <- clusters_three_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_3_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_three_tests_aov_pwc_3 <- cluster_three_tests_aov_pwc_3 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_three_tests_aov_pwc_3 %>% 
  reactable()
```


Row {.tabset}
-----------------------------------------------------------------------

### 4 Tests Cluster Summary - Test 1

```{r, include=FALSE}

clusters_four_impact_test_1 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_1"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_1 <- clusters_four_impact_test_1 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_1_scaled <- map_df(clusters_four_impact_test_1, rescale01b)

clusters_four_impact_test_1_scaled <- bind_cols(clusters_four_impact_test_1, 
                                             clusters_four_impact_test_1_scaled)

clusters_four_impact_test_1_scaled <- clusters_four_impact_test_1_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_1_scaled[sapply(clusters_four_impact_test_1_scaled, is.character)] <- lapply(clusters_four_impact_test_1_scaled[sapply(clusters_four_impact_test_1_scaled, is.character)], as.factor)

str(clusters_four_impact_test_1_scaled)

clusters_four_impact_test_1_long <- clusters_four_impact_test_1_scaled %>% 
    pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_1_score",
    names_pattern = "(.*)_score_post_injury_1"
  ) 

clusters_four_impact_test_2 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_2"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_2 <- clusters_four_impact_test_2 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_2_scaled <- map_df(clusters_four_impact_test_2, rescale01b)

clusters_four_impact_test_2_scaled <- bind_cols(clusters_four_impact_test_2, 
                                             clusters_four_impact_test_2_scaled)

clusters_four_impact_test_2_scaled <- clusters_four_impact_test_2_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_2_scaled[sapply(clusters_four_impact_test_2_scaled, is.character)] <- lapply(clusters_four_impact_test_2_scaled[sapply(clusters_four_impact_test_2_scaled, is.character)], as.factor)

str(clusters_four_impact_test_2_scaled)

clusters_four_impact_test_2_long <- clusters_four_impact_test_2_scaled %>% 
    pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_2_score",
    names_pattern = "(.*)_score_post_injury_2"
  ) 

clusters_four_impact_test_3 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_3"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_3 <- clusters_four_impact_test_3 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_3_scaled <- map_df(clusters_four_impact_test_3, rescale01b)

clusters_four_impact_test_3_scaled <- bind_cols(clusters_four_impact_test_3, 
                                             clusters_four_impact_test_3_scaled)

clusters_four_impact_test_3_scaled <- clusters_four_impact_test_3_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_3_scaled[sapply(clusters_four_impact_test_3_scaled, is.character)] <- lapply(clusters_four_impact_test_3_scaled[sapply(clusters_four_impact_test_3_scaled, is.character)], as.factor)

str(clusters_four_impact_test_3_scaled)

clusters_four_impact_test_3_long <- clusters_four_impact_test_3_scaled %>% 
    pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_3_score",
    names_pattern = "(.*)_score_post_injury_3"
  ) 


clusters_four_impact_test_4 <- four_impact_test %>% 
  select(c(1:20, contains("_post_injury_4"))) %>% 
    mutate(X1 = as.character(X1),
         row = as.character(row),
         age_post_injury_1 = as.character(age_post_injury_1),
         age_post_injury_2 = as.character(age_post_injury_2),
         age_post_injury_3 = as.character(age_post_injury_3),
         age_post_injury_4 = as.character(age_post_injury_4))

clusters_four_impact_test_4 <- clusters_four_impact_test_4 %>% 
  select(c(1:20, 44:49))

clusters_four_impact_test_4_scaled <- map_df(clusters_four_impact_test_4, rescale01b)

clusters_four_impact_test_4_scaled <- bind_cols(clusters_four_impact_test_4, 
                                             clusters_four_impact_test_4_scaled)

clusters_four_impact_test_4_scaled <- clusters_four_impact_test_4_scaled %>% 
  select(-c(21:26))

clusters_four_impact_test_4_scaled[sapply(clusters_four_impact_test_4_scaled, is.character)] <- lapply(clusters_four_impact_test_4_scaled[sapply(clusters_four_impact_test_4_scaled, is.character)], as.factor)

str(clusters_four_impact_test_4_scaled)

clusters_four_impact_test_4_long <- clusters_four_impact_test_4_scaled %>% 
    pivot_longer(
    cols = c(21:26),
    names_to = "symptom_cluster",
    values_to = "post_injury_4_score",
    names_pattern = "(.*)_score_post_injury_4"
  ) 


clusters_four_impact_test_join_1 <- left_join(clusters_four_impact_test_1_long,
                                               clusters_four_impact_test_2_long)

clusters_four_impact_test_join_2 <- left_join(clusters_four_impact_test_join_1, 
                                                 clusters_four_impact_test_3_long)

clusters_four_impact_test_complete <- left_join(clusters_four_impact_test_join_2,
                                                clusters_four_impact_test_4_long)

str(clusters_four_impact_test_complete)
```


```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_1_score)
```

### 4 Tests Cluster Boxplot - Test 1

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_1_score))
```

### 4 Tests Cluster ANOVA - Test 1

```{r, include=FALSE}
cluster_four_tests_aov_1 <- aov(post_injury_1_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_1)
```

### 4 Tests Cluster PWC - Test 1

```{r, include=FALSE}
cluster_four_tests_aov_pwc_1 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_1_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_1 <- cluster_four_tests_aov_pwc_1 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_1 %>% 
  reactable()
```


### 4 Tests Cluster Summary - Test 2

```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_2_score)
```

### 4 Tests Cluster Boxplot - Test 2

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_2_score))
```

### 4 Tests Cluster ANOVA - Test 2

```{r, include=FALSE}
cluster_four_tests_aov_2 <- aov(post_injury_2_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_2)
```

### 4 Tests Cluster PWC - Test 2

```{r, include=FALSE}
cluster_four_tests_aov_pwc_2 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_2_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_2 <- cluster_four_tests_aov_pwc_2 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_2 %>% 
  reactable()
```


### 4 Tests Cluster Summary - Test 3

```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_3_score)
```

### 4 Tests Cluster Boxplot - Test 3

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_3_score))
```

### 4 Tests Cluster ANOVA - Test 3

```{r, include=FALSE}
cluster_four_tests_aov_3 <- aov(post_injury_3_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_3)
```

### 4 Tests Cluster PWC - Test 3

```{r, include=FALSE}
cluster_four_tests_aov_pwc_3 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_3_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_3 <- cluster_four_tests_aov_pwc_3 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_3 %>% 
  reactable()
```


### 4 Tests Cluster Summary - Test 4

```{r, include=TRUE}
cluster_smry_tbl(clusters_four_impact_test_complete, post_injury_4_score)
```

### 4 Tests Cluster Boxplot - Test 4

```{r, include=TRUE}
ggplotly(cluster_bxp(clusters_four_impact_test_complete, 
                     symptom_cluster, post_injury_4_score))
```

### 4 Tests Cluster ANOVA - Test 4

```{r, include=FALSE}
cluster_four_tests_aov_4 <- aov(post_injury_4_score ~ symptom_cluster, 
                            data = clusters_four_impact_test_complete)
```

```{r, include=TRUE}
summary(cluster_four_tests_aov_4)
```

### 4 Tests Cluster PWC - Test 4

```{r, include=FALSE}
cluster_four_tests_aov_pwc_4 <- clusters_four_impact_test_complete %>% 
  pairwise_t_test(
    post_injury_4_score ~ symptom_cluster, paired = FALSE,
    p.adjust.method = "bonferroni"
  )

cluster_four_tests_aov_pwc_4 <- cluster_four_tests_aov_pwc_4 %>% 
  mutate_at(vars(p, p.adj), list(~ round(., 2)))
```

```{r, include=TRUE}
cluster_four_tests_aov_pwc_4 %>% 
  reactable()
```


# Gender-Cluster Interaction  

Sidebar {.sidebar}
------------



Row {.tabset}
-----------------------------------------------------------------------

### 1 Test Cluster Summary 

```{r, include=FALSE}
clusters_one_impact_test_scaled_long


scaled_cluster_gender_tbl <- function(df, a, b, c) {
  df %>% 
    group_by({{a}}, {{b}}) %>% 
    summarize(Mean = mean({{c}}),
              SD = sd({{c}}),
              Min = min({{c}}),
              Max = max({{c}})) %>%  
    mutate(Mean = as.numeric(Mean),
           SD = as.numeric(SD),
           Min = as.numeric(Min),
           Max = as.numeric(Max)) %>% 
    arrange(desc(Mean)) %>% 
    mutate_if(is.numeric, round, 2) %>% 
    reactable(
      defaultColDef = colDef(align = "center"),
      columns = list(
        gender = colDef(name = "Gender"),
        symptom_cluster = colDef(name = "Symptom Cluster"),
        Mean = colDef(format = colFormat(separators = TRUE)),
        SD = colDef(format = colFormat(separators = TRUE)),
        Min = colDef(format = colFormat(separators = TRUE)),
        Max = colDef(format = colFormat(separators = TRUE))
      ),
      pagination = TRUE,
      striped = TRUE,
      outlined = TRUE,
      compact = TRUE,
      highlight = TRUE,
      bordered = TRUE,
      searchable = TRUE
    )    
}

scaled_cluster_gender_tbl(clusters_one_impact_test_scaled_long,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)

```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_one_impact_test_scaled_long,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### 1 Test Cluster Boxplot 

```{r, include=FALSE}

ggplot(clusters_one_impact_test_scaled_long,
  aes(symptom_cluster, post_injury_1_score, color = gender)) +
  geom_boxplot() + 
  scale_color_OkabeIto() + 
  coord_flip() +
  labs(x = "",
       y = "Scaled Severity Score") +
  theme(legend.title = element_blank())

gender_cluster_bxp <- function(df, a, b, c) {
ggplot(df,
  aes({{a}}, {{b}}, color = {{c}})) +
  geom_boxplot() + 
  scale_color_OkabeIto() + 
  coord_flip() +
  labs(x = "",
       y = "Scaled Severity Score") +
  theme(legend.title = element_blank()) 
}


gender_cluster_bxp(clusters_one_impact_test_scaled_long,
                   symptom_cluster,
                   post_injury_1_score,
                   gender)

```

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_one_impact_test_scaled_long,
                            symptom_cluster,
                            post_injury_1_score,
                            gender)
```


### 1 Test Cluster Two-Way ANOVA 

```{r, include=FALSE}
cluster_one_test_aov2 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_one_impact_test_scaled_long)


library(car)

summary(cluster_one_test_aov2)

cluster_one_test_aov2

Anova(cluster_one_test_aov2, type = "III")

TukeyHSD(cluster_one_test_aov2)
TukeyHSD(cluster_one_test_aov2, which = "gender")
TukeyHSD(cluster_one_test_aov2, which = "symptom_cluster")


library(broom)

res_1 <- TukeyHSD(cluster_one_test_aov2)

tidy(res_1) %>% 
  reactable()

a_1 <- Anova(cluster_one_test_aov2, type = "III")

reactable(a_1)

```

```{r, include=TRUE}
reactable(a_1)
```

### 1 Test Cluster PWC

```{r, include=TRUE}
tidy(res_1) %>% 
  reactable()
```


Row {.tabset}
-----------------------------------------------------------------------

### 2 Tests Cluster Summary - Test 1

```{r, include=FALSE}
clusters_two_impact_test_complete[sapply(clusters_two_impact_test_complete, is.character)] <- lapply(clusters_two_impact_test_complete[sapply(clusters_two_impact_test_complete, is.character)], as.factor)

str(clusters_two_impact_test_complete)
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_two_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### 2 Tests Cluster Boxplot - Test 1

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_two_impact_test_complete,
                            symptom_cluster,
                            post_injury_1_score,
                            gender)
```

### 2 Tests Cluster Two-Way ANOVA - Test 1

```{r, include=FALSE}
cluster_two_tests_aov2_1 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_two_impact_test_complete)


summary(cluster_two_tests_aov2_1)

cluster_two_tests_aov2_1

Anova(cluster_two_tests_aov2_1, type = "III")

TukeyHSD(cluster_two_tests_aov2_1)


res_2_1 <- TukeyHSD(cluster_two_tests_aov2_1)


a_2_1 <- Anova(cluster_two_tests_aov2_1, type = "III")

```


```{r, include=TRUE}
reactable(a_2_1)
```

### 2 Tests Cluster PWC - Test 1

```{r, include=TRUE}
tidy(res_2_1) %>% 
  reactable()
```


### 2 Tests Cluster Summary - Test 2

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_two_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### 2 Tests Cluster Boxplot - Test 2

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_two_impact_test_complete,
                            symptom_cluster,
                            post_injury_2_score,
                            gender)
```

### 2 Tests Cluster Two-Way ANOVA - Test 2

```{r, include=FALSE}
cluster_two_tests_aov2_2 <- aov(post_injury_2_score ~ symptom_cluster * gender, 
                            data = clusters_two_impact_test_complete)


summary(cluster_two_tests_aov2_2)

cluster_two_tests_aov2_2

Anova(cluster_two_tests_aov2_2, type = "III")

TukeyHSD(cluster_two_tests_aov2_2)


res_2_2 <- TukeyHSD(cluster_two_tests_aov2_2)


a_2_2 <- Anova(cluster_two_tests_aov2_2, type = "III")

```


```{r, include=TRUE}
reactable(a_2_2)
```

### 2 Tests Cluster PWC - Test 1

```{r, include=TRUE}
tidy(res_2_2) %>% 
  reactable()
```


Row {.tabset}
-----------------------------------------------------------------------

### 3 Tests Cluster Summary - Test 1

```{r, include=FALSE}
clusters_three_impact_test_complete[sapply(clusters_three_impact_test_complete, is.character)] <- lapply(clusters_three_impact_test_complete[sapply(clusters_three_impact_test_complete, is.character)], as.factor)

str(clusters_three_impact_test_complete)
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_three_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### 3 Tests Cluster Boxplot - Test 1

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_three_impact_test_complete,
                            symptom_cluster,
                            post_injury_1_score,
                            gender)
```

### 3 Tests Cluster Two-Way ANOVA - Test 1

```{r, include=FALSE}
cluster_three_tests_aov2_1 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_three_impact_test_complete)


summary(cluster_three_tests_aov2_1)

cluster_three_tests_aov2_1

Anova(cluster_three_tests_aov2_1, type = "III")

TukeyHSD(cluster_three_tests_aov2_1)

res_3_1 <- TukeyHSD(cluster_three_tests_aov2_1)


a_3_1 <- Anova(cluster_three_tests_aov2_1, type = "III")

```


```{r, include=TRUE}
reactable(a_3_1)
```

### 3 Tests Cluster PWC - Test 1

```{r, include=TRUE}
tidy(res_3_1) %>% 
  reactable()
```


### 3 Tests Cluster Summary - Test 2

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_three_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### 3 Tests Cluster Boxplot - Test 2

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_three_impact_test_complete,
                            symptom_cluster,
                            post_injury_2_score,
                            gender)
```

### 3 Tests Cluster Two-Way ANOVA - Test 2

```{r, include=FALSE}
cluster_three_tests_aov2_2 <- aov(post_injury_2_score ~ symptom_cluster * gender, 
                            data = clusters_three_impact_test_complete)


summary(cluster_three_tests_aov2_2)

cluster_three_tests_aov2_2

Anova(cluster_three_tests_aov2_2, type = "III")

TukeyHSD(cluster_three_tests_aov2_2)


res_3_2 <- TukeyHSD(cluster_three_tests_aov2_2)


a_3_2 <- Anova(cluster_three_tests_aov2_2, type = "III")

```


```{r, include=TRUE}
reactable(a_3_2)
```

### 3 Tests Cluster PWC - Test 2

```{r, include=TRUE}
tidy(res_3_2) %>% 
  reactable()
```


### 3 Tests Cluster Summary - Test 3

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_three_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_3_score)
```

### 3 Tests Cluster Boxplot - Test 3

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_three_impact_test_complete,
                            symptom_cluster,
                            post_injury_3_score,
                            gender)
```

### 3 Tests Cluster Two-Way ANOVA - Test 3

```{r, include=FALSE}
cluster_three_tests_aov2_3 <- aov(post_injury_3_score ~ symptom_cluster * gender, 
                            data = clusters_three_impact_test_complete)


summary(cluster_three_tests_aov2_3)

cluster_three_tests_aov2_3

Anova(cluster_three_tests_aov2_3, type = "III")

TukeyHSD(cluster_three_tests_aov2_3)


res_3_3 <- TukeyHSD(cluster_three_tests_aov2_3)


a_3_3 <- Anova(cluster_three_tests_aov2_3, type = "III")

```


```{r, include=TRUE}
reactable(a_3_3)
```

### 3 Tests Cluster PWC - Test 3

```{r, include=TRUE}
tidy(res_3_3) %>% 
  reactable()
```


Row {.tabset}
-----------------------------------------------------------------------

### 4 Tests Cluster Summary - Test 1

```{r, include=FALSE}
clusters_four_impact_test_complete[sapply(clusters_four_impact_test_complete, is.character)] <- lapply(clusters_four_impact_test_complete[sapply(clusters_four_impact_test_complete, is.character)], as.factor)

str(clusters_four_impact_test_complete)
```

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_1_score)
```

### 4 Tests Cluster Boxplot - Test 1

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_1_score,
                            gender)
```

### 4 Tests Cluster Two-Way ANOVA - Test 1

```{r, include=FALSE}
cluster_four_tests_aov2_1 <- aov(post_injury_1_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_1)

cluster_four_tests_aov2_1

Anova(cluster_four_tests_aov2_1, type = "III")

TukeyHSD(cluster_four_tests_aov2_1)

res_4_1 <- TukeyHSD(cluster_four_tests_aov2_1)


a_4_1 <- Anova(cluster_four_tests_aov2_1, type = "III")

```


```{r, include=TRUE}
reactable(a_4_1)
```

### 4 Tests Cluster PWC - Test 1

```{r, include=TRUE}
tidy(res_4_1) %>% 
  reactable()
```


### 4 Tests Cluster Summary - Test 2

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_2_score)
```

### 4 Tests Cluster Boxplot - Test 2

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_2_score,
                            gender)
```

### 4 Tests Cluster Two-Way ANOVA - Test 2

```{r, include=FALSE}
cluster_four_tests_aov2_2 <- aov(post_injury_2_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_2)

cluster_four_tests_aov2_2

Anova(cluster_four_tests_aov2_2, type = "III")

TukeyHSD(cluster_four_tests_aov2_2)


res_4_2 <- TukeyHSD(cluster_four_tests_aov2_2)


a_4_2 <- Anova(cluster_four_tests_aov2_2, type = "III")

```


```{r, include=TRUE}
reactable(a_4_2)
```

### 4 Tests Cluster PWC - Test 2

```{r, include=TRUE}
tidy(res_4_2) %>% 
  reactable()
```


### 4 Tests Cluster Summary - Test 3

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_3_score)
```

### 4 Tests Cluster Boxplot - Test 3

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_3_score,
                            gender)
```

### 4 Tests Cluster Two-Way ANOVA - Test 3

```{r, include=FALSE}
cluster_four_tests_aov2_3 <- aov(post_injury_3_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_3)

cluster_four_tests_aov2_3

Anova(cluster_four_tests_aov2_3, type = "III")

TukeyHSD(cluster_four_tests_aov2_3)


res_4_3 <- TukeyHSD(cluster_four_tests_aov2_3)


a_4_3 <- Anova(cluster_four_tests_aov2_3, type = "III")

```


```{r, include=TRUE}
reactable(a_4_3)
```

### 4 Tests Cluster PWC - Test 3

```{r, include=TRUE}
tidy(res_4_3) %>% 
  reactable()
```


### 4 Tests Cluster Summary - Test 4

```{r, include=TRUE}
scaled_cluster_gender_tbl(clusters_four_impact_test_complete,
                          gender,
                          symptom_cluster,
                          post_injury_4_score)
```

### 4 Tests Cluster Boxplot - Test 4

```{r, include=TRUE, fig.width=10}
gender_cluster_bxp(clusters_four_impact_test_complete,
                            symptom_cluster,
                            post_injury_4_score,
                            gender)
```

### 4 Tests Cluster Two-Way ANOVA - Test 4

```{r, include=FALSE}
cluster_four_tests_aov2_4 <- aov(post_injury_4_score ~ symptom_cluster * gender, 
                            data = clusters_four_impact_test_complete)


summary(cluster_four_tests_aov2_4)

cluster_four_tests_aov2_4

Anova(cluster_four_tests_aov2_4, type = "III")

TukeyHSD(cluster_four_tests_aov2_4)


res_4_4 <- TukeyHSD(cluster_four_tests_aov2_4)


a_4_4 <- Anova(cluster_four_tests_aov2_4, type = "III")

```


```{r, include=TRUE}
reactable(a_4_4)
```

### 4 Tests Cluster PWC - Test 4

```{r, include=TRUE}
tidy(res_4_4) %>% 
  reactable()
```